//
//  QNService.m
//  QNReuseTool
//
//  Created by rsh on 2019/10/15.
//  Copyright © 2019年 rsh. All rights reserved.
//

#import "QNService.h"

@implementation QNService

static NSArray *_services;

+ (void)serviceLauchWithServices:(NSArray <QNService *> *)services options:(NSDictionary *)options {
    [services enumerateObjectsUsingBlock:^(QNService * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj respondsToSelector:@selector(service:launchWithOptions:)]) {
            [obj service:obj launchWithOptions:options];
        } else {
            NSString *assert = [NSString stringWithFormat:@"WARNING：Please create subClass of QNService and implement '- (void)service:(QNService *)service launchWithOptions:(NSDictionary *)options.'"];
            NSAssert(0, assert);
        }
    }];
    _services = services;
}

- (void)service:(QNService *)service launchWithOptions:(NSDictionary *)options {
    NSString *assert = [NSString stringWithFormat:@"WARNING：Please implement %@ '- (void)service:(QNService *)service launchWithOptions:(NSDictionary *)options.'", service];
    NSAssert(0, assert);
}

+ (void)serviceDidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [_services enumerateObjectsUsingBlock:^(QNService * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj respondsToSelector:@selector(service:didRegisterForRemoteNotificationsWithDeviceToken:)]) {
            [obj service:obj didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
            *stop = YES;
        }
    }];
}

+ (void)serviceDidFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    [_services enumerateObjectsUsingBlock:^(QNService * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj respondsToSelector:@selector(service:didFailToRegisterForRemoteNotificationsWithError:)]) {
            [obj service:obj didFailToRegisterForRemoteNotificationsWithError:error];
            *stop = YES;
        }
    }];
}

+ (void)serviceReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    [_services enumerateObjectsUsingBlock:^(QNService * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj respondsToSelector:@selector(service:didReceiveRemoteNotification:fetchCompletionHandler:)]) {
            [obj service:obj didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
            *stop = YES;
        }
    }];
}

+ (void)serviceReceiveRemoteWithPresentNotification:(UNNotification *)notification {
    [_services enumerateObjectsUsingBlock:^(QNService * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj respondsToSelector:@selector(service:didReceiveRemoteWithPresentNotification:)]) {
            [obj service:obj didReceiveRemoteWithPresentNotification:notification];
            *stop = YES;
        }
    }];
}

+ (void)serviceReceiveRemoteWithPresentNotificationResponse:(UNNotificationResponse *)response {
    [_services enumerateObjectsUsingBlock:^(QNService * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj respondsToSelector:@selector(service:didReceiveRemoteWithPresentNotificationResponse:)]) {
            [obj service:obj didReceiveRemoteWithPresentNotificationResponse:response];
            *stop = YES;
        }
    }];
}

+ (BOOL)serviceHandleOpenURL:(NSURL *)url {
    __block BOOL result;
    [_services enumerateObjectsUsingBlock:^(QNService * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj respondsToSelector:@selector(service:openURL:)]) {
            result = [obj service:obj openURL:url];
            *stop = YES;
        }
    }];
    
    return result;
}

+ (void)setJPUSHAlias:(NSString *)jpushAlias {
    [_services enumerateObjectsUsingBlock:^(QNService * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj respondsToSelector:@selector(service:jpushAlias:)]) {
           [obj service:obj jpushAlias:jpushAlias];
            *stop = YES;
        }
    }];
}

+ (void)deleteJPUSHAlias {
    [_services enumerateObjectsUsingBlock:^(QNService * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj respondsToSelector:@selector(deleteJPUSHAlias:)]) {
            [obj deleteJPUSHAlias:obj];
            *stop = YES;
        }
    }];
}

+ (void)resetBadge
{
    [_services enumerateObjectsUsingBlock:^(QNService * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj respondsToSelector:@selector(resetBadge:)]) {
            [obj resetBadge:obj];
            *stop = YES;
        }
    }];
}

@end
